package com.agriculture.data_demo.service;

import com.agriculture.data_demo.dto.PageResult;
import com.agriculture.data_demo.entity.sys.HenBatchEntity;
import com.agriculture.data_demo.entity.sys.HenDailyEntity;
import com.agriculture.data_demo.repository.HenBatchRepository;
import com.agriculture.data_demo.repository.HenDailyRepository;
import com.agriculture.data_demo.vo.sys.HenBatchVo;
import com.agriculture.data_demo.vo.sys.HenDailyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author morningbear
 * @date 20.10.8
 * @update 20.10.19
 */
@Slf4j
@Service
public class HenService {

    @Resource
    private HenBatchRepository henBatchRepository;
    @Resource
    private HenDailyRepository henDailyRepository;

    /**
     *
     * @param startDate 起始日期
     * @param endDate 截止日期
     * @param pageNumber 请求页数
     * @param pageSize 页大小
     * @return 一段时间内的蛋鸡生产周期数据
     */
    public PageResult<HenDailyVo> getHenDailyByDate(Date startDate, Date endDate, Integer pageNumber, Integer pageSize){
        Page<HenDailyEntity> page = henDailyRepository.findHenDailyEntitiesByDate(startDate, endDate, PageRequest.of(pageNumber, pageSize));
        return getHenDailyVoPageResult(pageNumber, pageSize, page);
    }

    /**
     *
     * @param pageNumber 请求页数
     * @param pageSize 页大小
     * @return 蛋鸡生产周期数据
     */
    public PageResult<HenDailyVo> getAllHenDaily(Integer pageNumber, Integer pageSize){
        Page<HenDailyEntity> page = henDailyRepository.findAll(PageRequest.of(pageNumber, pageSize));

        return getHenDailyVoPageResult(pageNumber, pageSize, page);
    }


    private PageResult<HenDailyVo> getHenDailyVoPageResult(Integer pageNumber, Integer pageSize, Page<HenDailyEntity> page) {
        List<HenDailyVo> henDailyVoList = new ArrayList<>();

        for (HenDailyEntity henDailyEntity : page.getContent()){
            HenDailyVo henDailyVo = new HenDailyVo();
            BeanUtils.copyProperties(henDailyEntity, henDailyVo);
            henDailyVoList.add(henDailyVo);
        }
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), henDailyVoList);
    }

    /**
     *
     * @param pageNumber 请求页数
     * @param pageSize 页大小
     * @return 蛋鸡生产批次数据
     */
    public PageResult<HenBatchVo> getAllHenBatch(Integer pageNumber, Integer pageSize){
        Page<HenBatchEntity> page = henBatchRepository.findAll(PageRequest.of(pageNumber, pageSize));

        return getHenBatchVoPageResult(pageNumber, pageSize, page);
    }

    /**
     *
     * @param startDate 起始日期
     * @param endDate 截至日期
     * @param pageNumber 请求页数
     * @param pageSize 请求页大小
     * @return 一段时间内的蛋鸡生产批次数据
     */
    public PageResult<HenBatchVo> getHenBatchByDate(Date startDate, Date endDate, Integer pageNumber, Integer pageSize){
        Page<HenBatchEntity> page = henBatchRepository.findHenBatchEntitiesByDate(startDate, endDate, PageRequest.of(pageNumber, pageSize));

        return getHenBatchVoPageResult(pageNumber, pageSize, page);
    }

    private PageResult<HenBatchVo> getHenBatchVoPageResult(Integer pageNumber, Integer pageSize, Page<HenBatchEntity> page) {
        List<HenBatchVo> henBatchVoList = new ArrayList<>();
        for (HenBatchEntity henBatchEntity : page.getContent()){
            HenBatchVo henBatchVo = new HenBatchVo();
            BeanUtils.copyProperties(henBatchEntity, henBatchVo);
            henBatchVoList.add(henBatchVo);
        }
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), henBatchVoList);
    }
}
